import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import liveHome from '../model/live/liveHome';
import { clientBase } from './core/clientBase';
import returnModel from '../model/common/returnModel';
import areaList, { areaListItem } from '../model/live/areaList';
import { emptyData } from '../model/common/emptyData';

class liveHomeClient extends clientBase {
  constructor() {
    super();
  }

  loadHome(): Promise<returnModel<liveHome>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.live.bilibili.com/xlive/app-interface/v2/index/getAllList", `&actionKey=appkey&qn=0&rec_page=1&relation_page=1&scale=xxhdpi`);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data = res.data as liveHome;
            resolve(this.buildSuccessResult<liveHome>(data));
          } else {
            let failed = this.buildFailedResult<liveHome>(res.message);
            resolve(failed);
          }
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<liveHome>(e));
      }
    });
  }

  getAreaList(): Promise<returnModel<Array<areaList>>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.live.bilibili.com/room/v1/Area/getList", "&actionKey=appkey&need_entrance=1&parent_id=0");
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data = res.data as Array<areaList>;
            this.getRecAreaList().then((res) => {
              if (res.success) {
                let item = new areaList();
                item.list = res.data;
                data.push(item);
              }
              resolve(this.buildSuccessResult<Array<areaList>>(data));
            }).catch(() => resolve(this.buildSuccessResult<Array<areaList>>(data)));
          } else
            resolve(this.buildFailedResult<Array<areaList>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<areaList>>(e));
      }
    });
  }

  private getRecAreaList(): Promise<returnModel<Array<areaListItem>>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.live.bilibili.com/room/v1/Area/getRecList", "&actionKey=appkey");
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<areaListItem> = res.data;
            resolve(this.buildSuccessResult<Array<areaListItem>>(data));
          } else
            resolve(this.buildFailedResult<Array<areaListItem>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<Array<areaListItem>>(e));
      }
    });
  }

  getHot(hotIndex: number): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.live.bilibili.com/room/v3/Area/getRoomList",
          `&actionKey=appkey&area_id=0&cate_id=0&page=${hotIndex}&page_size=30&parent_area_id=0&qn=0&sort_type=online&tag_version=1`, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }

  getNewData(newIndex: number): Promise<returnModel<emptyData>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://api.live.bilibili.com/room/v3/Area/getRoomList",
          `&actionKey=appkey&area_id=0&cate_id=0&page=${newIndex}&page_size=30&parent_area_id=0&qn=0&sort_type=live_time&tag_version=1`, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: emptyData = res.data;
            resolve(this.buildSuccessResult<emptyData>(data));
          } else
            resolve(this.buildFailedResult<emptyData>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<emptyData>(e));
      }
    });
  }
}
export default new liveHomeClient();